/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wargame.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import wargame.dao.*;
import wargame.entity.*;
import module.*;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Clarence
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {

    @RequestMapping(value = "/Login.do", method = RequestMethod.POST)
    public ModelAndView loginGame(@RequestParam(value = "player_name", required = true) String player_name, @RequestParam(value = "password", required = true) String password, HttpServletRequest request, HttpServletResponse response, Model model) {

        ModelAndView mv = new ModelAndView();

        if (password.equals("password1")) {
            // Correct Password
            Player p = PlayerDAO.retrievePlayerByName(player_name);
            if (p != null) {
                // Create Player Session
                PlayerSession ps = new PlayerSession();
                ps.setPlayer_id(p.getPlayer_id());

                // insert player into session
                HttpSession session = request.getSession(true);
                session.setAttribute(GlobalConstant.PLAYER_SESSION, ps);
                mv.addObject("announcement", "Login Success");
                mv.setViewName("redirect:../servlet/player/dashboard");
                return mv;
            }

        }

        mv.addObject("announcement", "Login fails. username / password not correct");
        mv.setViewName("register");

        return mv;
    }
}
